package com.ahao.service;

import com.ahao.dao.CourseDao;
import com.ahao.model.Course;
import com.ahao.utils.CommonsUtils;

import java.sql.SQLException;
import java.util.List;

public class CourseService {
    CourseDao courseDao = new CourseDao();

    /**
     * 查询所有课程
     * @return
     */

    public List<Course> getAllCourse() {
        try {
            return courseDao.getAllCourses();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *  根据课程名查询课程
     * @param coursrName
     * @return
     */
    public Integer addCourse(String coursrName) {
        Course course = null;
        try {
            course = courseDao.getCourseByName(coursrName);
            if (course == null) {
                Integer integer = courseDao.addCourse(coursrName);
                return integer==1 ? CommonsUtils.UPDATE_SUCCESS :CommonsUtils.UPDATE_FAIL;
            }else {
                return CommonsUtils.UPDATE_FAIL;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return CommonsUtils.OTHER_EXCEPTION;
    }


    /**
     * 根据课程id删除课程
     * @param cid
     * @return
     */
    public Integer deleteCourseByCid(Integer cid) {
        try {
            Course course = courseDao.selectCourseByCid(cid);
            if (course != null) {
                Integer integer = courseDao.deleteCourseByCid(cid);
                return integer==1 ? CommonsUtils.UPDATE_SUCCESS :CommonsUtils.UPDATE_FAIL;
            }else {
                return CommonsUtils.ID_NOT_FIND;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return CommonsUtils.OTHER_EXCEPTION;
    }

    public List<Course> getCoursesByGid(String gid) {
        try {
            return courseDao.getCoursesByGid(Integer.parseInt(gid));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
